package com.example.mongo.appmongo.config;

import com.mongodb.ConnectionString;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.MongoTransactionManager;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoClientDbFactory;

/**
 * @author sjw
 * @className MongoConfig
 * @date 2021/9/29 15:30
 * @description: TODO
 */
@Configuration
public class MongoConfig implements InitializingBean {


//    @Bean(name ="mongoDatabaseFactory1" )
//    MongoDatabaseFactory mongoDatabaseFactory1(){
//        ConnectionString  connectionString= new ConnectionString("mongodb://dbadmin:qj123456789@192.168.10.127:27017,192.168.10.127:27018,192.168.10.127:27019/daily_report?repliaSet=rs0");
//        MongoClient client= MongoClients.create(connectionString);
//        TenantContext.putMongoClient("transactionManager1",client);
//        UserSimpleMongoClientDatabaseFactory factory= new UserSimpleMongoClientDatabaseFactory(client,connectionString.getDatabase());
//        TenantContext.putDatabaseFactory("transactionManager1",factory);
//        return factory;
//    }


//    @Bean()
//    MongoTransactionManager transactionManager1(@Qualifier("mongoDatabaseFactory1") MongoDatabaseFactory factory) {
//
////        ConnectionString  connectionString= new ConnectionString("mongodb://dbadmin:qj123456789@192.168.10.127:27017,192.168.10.127:27018,192.168.10.127:27019/daily_report?repliaSet=rs0");
////        MongoClient client= MongoClients.create(connectionString);
////        TenantContext.putMongoClient("transactionManager1",client);
////        return new MongoTransactionManager(new UserSimpleMongoClientDatabaseFactory(client,connectionString.getDatabase()));
////
//        return new MongoTransactionManager(factory);
//    }


//    @Bean
//    @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
//    public TransactionAttributeSource transactionAttributeSource() {
//        return new UserAnnotationTransactionAttributeSource();
//    }

//    @Primary
//    @Bean(name = "mongoDatabaseFactory")
    @Bean()
    MongoDbFactory MongoDbFactory() {
        ConnectionString connectionString = new ConnectionString("mongodb://dbadmin:qj123456789@192.168.10.200:27017/daily_report");
        MongoClient client = MongoClients.create(connectionString);

        MongoDbFactory factory = new SimpleMongoClientDbFactory(client, connectionString.getDatabase());

        return factory;
//        return new UserSimpleMongoClientDatabaseFactory(client,connectionString.getDatabase());
    }


    @Bean()
    MongoTemplate mongoTemplate(MongoDbFactory mongoDbFactory) {
        return new MongoTemplate(mongoDbFactory);
//        return new UserSimpleMongoClientDatabaseFactory(client,connectionString.getDatabase());
    }


    @Bean
    MongoTransactionManager transactionManager(MongoDbFactory mongoDbFactory) {
//        ConnectionString  connectionString= new ConnectionString("mongodb://dbadmin:qj123456789@192.168.10.127:27017,192.168.10.127:27018,192.168.10.127:27019/daily_report?repliaSet=rs0");
//        MongoClient client= MongoClients.create(connectionString);
//        TenantContext.putMongoClient("transactionManager",client);
//        return new MongoTransactionManager(new UserSimpleMongoClientDatabaseFactory(client,connectionString.getDatabase()));

        return new MongoTransactionManager(mongoDbFactory);
    }


//    @Bean
//    MongoTransactionManager transactionManager(@Qualifier("mongoDatabaseFactory") MongoDatabaseFactory factory) {
////        ConnectionString  connectionString= new ConnectionString("mongodb://dbadmin:qj123456789@192.168.10.127:27017,192.168.10.127:27018,192.168.10.127:27019/daily_report?repliaSet=rs0");
////        MongoClient client= MongoClients.create(connectionString);
////        TenantContext.putMongoClient("transactionManager",client);
////        return new MongoTransactionManager(new UserSimpleMongoClientDatabaseFactory(client,connectionString.getDatabase()));
//
//        return new MongoTransactionManager(factory);
//    }

    @Override
    public void afterPropertiesSet() throws Exception {

    }


//    @Bean
//    UserMongoTemplate mongoTemplate(@Qualifier("mongoDatabaseFactory") MongoDatabaseFactory factory){
//        return new UserMongoTemplate(factory);
//    }
//
//
//    @Autowired
//    ApplicationContext applicationContext;
//
//    @Override
//    public void afterPropertiesSet() throws Exception {
//        TenantContext.applicationContext=applicationContext;
//    }
}
